package com.ishansong.gis.tool;

import org.bson.BsonDocument;
import org.bson.BsonString;
import org.bson.BsonValue;

/**
 * Created by danqiang on 2017/7/6.
 * order_location_info导数据
 */

class Import implements MTProcessor.Processor {

    private Commander.ArgsHolder commanderArgsHolder;
    private boolean verbose = false;
    private Perf perf = new Perf();

    Import(Commander.ArgsHolder commanderArgsHolder) {
        this.commanderArgsHolder = commanderArgsHolder;
        this.verbose = commanderArgsHolder.verbose;
    }

    public void process(Mongo newMongo, BsonDocument bsonDocument) {
        BsonDocument newDocument = new BsonDocument();
        BsonValue fromPosition = bsonDocument.get("fromPosition");
        BsonValue toPosition = bsonDocument.get("toPosition");
        BsonValue _id = bsonDocument.get("_id");
        BsonValue orderId = bsonDocument.get("orderId");
        BsonValue cityId = bsonDocument.get("cityId");
        BsonValue status = bsonDocument.get("status");
        BsonValue createTime = bsonDocument.get("createTime");
        newDocument
                .append("_class", new BsonString("com.ishansong.location.model.OrderLocationInfo"))
                .append("_id", _id)
                .append("orderId", orderId)
                .append("cityId", cityId)
                .append("status", status)
                .append("createTime", createTime)
                .append("fromPosition", fromPosition)
                .append("toPosition", toPosition);

        BsonDocument criteria = new BsonDocument("orderId", orderId).append("_id", _id).append("createTime", createTime);
        newMongo.customUpsert(criteria, newDocument);

        if (this.verbose) {
            System.out.println("orderId: " + orderId + ", _id: " + _id + ", createTime: " + createTime);
        }

        perf.out();
    }

    @Override
    public String getOldDbName() {
        return "ss_location";
    }

    @Override
    public String getNewDbName() {
        return "ss_location";
    }

    @Override
    public String getOldCollectionName() {
        return "order_location_info_";
    }

    @Override
    public String getNewCollectionName() {
        return "order_location_info";
    }

    @Override
    public String toString() {
        return commanderArgsHolder.toString();
    }

}
